<?php
namespace app\api\model;

use think\Model;

class Userticket extends Model
{
    //查询用户有无对应商品的优惠券
    public function OneData($userid,$issueid,$money){
        $res=$this->alias('a')
            ->join('ticket','a.ticketid=ticket.id')
            ->where(['userid'=>$userid,'issueid'=>$issueid])
            ->field('a.*,ticket.min,ticket.money,ticket.stoptime')
            ->find();
        //判断有无此类优惠券
        if($res){
            //判断此优惠券是否满足使用条件
            if($res['min']<=$money&&$res['stoptime']<date('Y-m-d H:i:s',time())&&$res['status']==0){
                $arr=$this->where(['id',$res['id']])->update(['status'=>1]);
                $data=[$money-$res['money'],$res['ticketid']];
                return $data;
            }else{
                $data=[$money,''];
                return $data;
            }
        }else{
            $data=[$money,''];
            return $data;
        }
    }
    //取消订单是优惠券恢复使用
    public function qx($id){
        $arr=$this->where('id',$id)->update(['status'=>0]);
        return $arr;
    }
}